const express = require('express')
const logger = require('./logger')
const cors = require('cors')

const app = express()

//导入路由模块
const dashBoardRouter = require('./router/dashBoard.js')
const projectRouter = require('./router/project.js')
const dueOptRouter = require('./router/dueOpt.js')
const aiRouter = require('./router/ai.js')
const dataWarehouseRouter = require('./router/dataWarehouse.js')
const configRouter = require('./router/config.js')

app.use(cors())
app.use(logger)
app.use('/api/bi/ecsFile',express.static('api/bi/ecsFile'))
//使用app.use()注册路由模块
app.use('/api/bi/dashBoard', dashBoardRouter)
app.use('/api/bi/project', projectRouter)
app.use('/api/bi/due/opt', dueOptRouter)
app.use('/api/bi/ai', aiRouter)
app.use('/api/bi/data/warehouse', dataWarehouseRouter)
app.use('/api/bi', configRouter)

// 解决跨域问题
// app.all的第一个参数传*就代表所有接口都走下面的规则，也可以写其他的形式，比如：
// "/users/*" : 就代表users下的接口都走下面的配置规则
app.all("*", function (req, res, next) {
    // 设置允许跨域的域名,如果传*代表允许任意域名跨域
    // 下面这里是配置了允许http://localhost:8000进行跨域请求
    res.header('Access-Control-Allow-Origin', '*');
    // 允许的header类型
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    // 跨域允许的请求方式
    res.header('Access-Control-Allow-Methods', '*');
    if (req.method.toLowerCase() == 'options') {
      res.send(200); // 让options 尝试请求快速结束
    } else {
      next();
    }
});

app.listen(5003, () => {
  console.log('服务已启动...');
})